const app = getApp()
const util = require("../../utils/util.js")
const {
  $Message
} = require('../../dist/base/index');
const {
  $Toast
} = require('../../dist/base/index');

Page({
  data: {
    oneChecked: false,
    groups: [],
    startDate: '',
    startTime: '',
    endDate: '',
    endTime: '',
    records: [],
    loading: false,
    spinShow: false,
    addressIds: [],
    fileUrl: ''
  },
  onLoad: function(options) {
    let that = this
    let now = new Date()
    this.setData({
      startDate: util.formatTime(now, 1),
      startTime: util.formatTime(now, 2),
      endDate: util.formatTime(now, 1),
      endTime: util.formatTime(now, 2)
    })
    wx.cloud.callFunction({
      name: 'getSubGroup',
      data: {
        type: '1',
        groupId: app.globalData.group._id
      }
    }).then(res => {
      that.setData({
        groups: res.result
      })
    })
  },
  onChange(event) {
    let index = event.currentTarget.dataset.index.toString()
    let detail = event.detail
    this.setData({
      ['groups[' + index + ']' + '.address' + '[' + event.detail.name + '].checked']: detail.checked
    })
  },
  query() {
    let that = this
    if (!this.judgeValid()) {
      return
    }
    this.setData({
      loading: true
    })
    wx.cloud.callFunction({
      name: 'queryRegistration',
      data: {
        addressIds: that.data.addressIds,
        startDate: that.data.startDate,
        startTime: that.data.startTime,
        endDate: that.data.endDate,
        endTime: that.data.endTime
      }
    }).then(res => {
      that.setData({
        records: res.result.list,
        loading: false
      })
    })
  },
  recordExport() {
    let that = this
    if (!this.judgeValid()) {
      return
    }
    this.setData({
      spinShow: true
    })
    wx.cloud.callFunction({
      name: 'queryRegistration',
      data: {
        addressIds: that.data.addressIds,
        startDate: that.data.startDate,
        startTime: that.data.startTime,
        endDate: that.data.endDate,
        endTime: that.data.endTime
      }
    }).then(res => {
      if (res.result.list.length == 0) {
        this.setData({
          spinShow: false
        })
        $Toast({
          content: '记录为空',
          image: "../../images/icon/sad.png"
        });
      } else {
        that.savaExcel(res.result.list)
      }
    })
  },
  judgeValid() {
    if (!util.judgeTime(this.data.startDate, this.data.startTime, this.data.endDate, this.data.endTime)) {
      $Message({
        content: '时间段不对',
        type: 'warning',
        duration: 1
      });
      return false
    }
    let addressIds = []
    for (let i = 0; i < this.data.groups.length; i++) {
      for (let j = 0; j < this.data.groups[i].address.length; j++) {
        if (this.data.groups[i].address[j].checked) {
          addressIds.push(this.data.groups[i].address[j]._id)
        }
      }
    }
    if (addressIds.length == 0) {
      $Message({
        content: '请选择出入口',
        type: 'warning',
        duration: 1
      });
      return false
    }
    this.setData({
      addressIds: addressIds
    })
    return true
  },
  viewDetail(e) {
    wx.navigateTo({
      url: '/pages/record_detail/record_detail?id=' + e.currentTarget.dataset.record._id,
    })
  },
  savaExcel(datas) {
    let that = this
    wx.cloud.callFunction({
      name: "excel",
      data: {
        datas: datas
      },
      success(res) {
        that.getFileUrl(res.result.fileID)
        that.setData({
          spinShow: false
        })
        $Toast({
          content: '导出成功',
          image: "../../images/icon/happy.png"
        });
      },
      fail(res) {
        console.log("保存失败", res)
      }
    })
  },
  getFileUrl(fileID) {
    let that = this;
    wx.cloud.getTempFileURL({
      fileList: [fileID],
      success: res => {
        that.setData({
          fileUrl: res.fileList[0].tempFileURL
        })
      },
      fail: err => { }
    })
  },
  copyFileUrl() {
    let that = this
    wx.setClipboardData({
      data: that.data.fileUrl,
      success(res) {
        wx.getClipboardData({
          success(res) { }
        })
      }
    })
  },
  bindStartDateChange: function(e) {
    this.setData({
      startDate: e.detail.value
    })
  },
  bindStartTimeChange: function(e) {
    this.setData({
      startTime: e.detail.value
    })
  },
  bindEndDateChange: function(e) {
    this.setData({
      endDate: e.detail.value
    })
  },
  bindEndtTimeChange: function(e) {
    this.setData({
      endtTime: e.detail.value
    })
  }
})